package com.llc.gulimall.order.listener;

import com.llc.common.to.mq.SeckillOrderTo;
import com.llc.gulimall.order.entity.OrderEntity;
import com.llc.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author llc
 * @Description
 * @Version
 * @date 2022/2/21 1:38
 */
@Slf4j
@RabbitListener(queues = "order.seckill.order.queue")
@Component
public class OrderSeckillListener {

    @Autowired
    OrderService orderService;
    @RabbitHandler
    public void listener(SeckillOrderTo seckillOrder, Channel channel, Message message) throws IOException {
        //System.out.println("收到过期的订单消息,准备关闭订单" + entity);
        try {
            log.info("准备创建秒杀订单的详细信息");
            orderService.createSeckillOrder(seckillOrder);
            System.out.println("关闭订单");
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            System.out.println("成功");
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }
}
